<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        let _toString = function(obj){
            return Object.prototype.toString.call(obj)
        }

        let extend = function(target,...settings){
            let isDeepClone = false,
            types = {
                Date(key,value){
                    this[key] = new Date(value);
                },
                RegExp(key,value){
                    this[key] = new RegExp(value);
                }
            },
            factory = function(key,value){
                let type = /\[object (\w+)\]/.exec(_toString(value))[1];
                //先判断是否是日期或正则
                if(types[type]){
                    types[type].call(this,key,value);
                //对象类型    
                }else if(typeof value == 'object'){
                    target = Array.isArray(value) ? target[key]||[] : target[key]||{};
                    this[key] = extend(isDeepClone,target,value);
                //值类型,直接在目标对象上加属性
                }else{
                    this[key] = value;
                }
            },
            clone = function(){ 
                return Object.assign.apply(Object,settings);
            },
            deepClone = function(target,settings) {
                settings.forEach(setting=>{
                    let keys = Object.keys(setting);
                    keys.forEach(key=>{
                        let value = setting[key];
                        factory.call(target,key,value);
                    })
                });
                return target;
            };
            typeof target == 'boolean' && (isDeepClone = true,target = settings.shift())
            //浅拷贝
            if(!isDeepClone){
                settings.unshift(target);
                return clone();
            }
            return deepClone(target,settings);
        }

        var obj = {};
        var dftOptions = {
            storageKey:'storageId',
            type:'localStorage',
            clone:{
                isEnable:'auto',
                cloneType:'assign'
            }
        }
        var object2 = {
            storageKey:'sys',
            clone:{
                cloneType:'deep',
                lang:'js'
            }
        };

        console.log(extend(true,dftOptions,object2))
    </script>
</body>
</html>